﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twitter;
using System.Configuration;
using System.Diagnostics;
using FriendsNearby.Services;

namespace FriendsNearbyBot
{
	class Program
	{
		static void Main(string[] args)
		{
			// 1. Mutex created in global name space. See http://msdn2.microsoft.com/en-us/library/ms682411.aspx
			// 2. Guid for FriendsNearbyBot Application
			var runner = new SingleInstanceRunner(@"Global\121827C4-301E-404b-A6BD-5C75BB1EA3E2", RunBot, BotAlreadyRunning);

			runner.Run();

			if (Debugger.IsAttached)
			{
				Console.ReadLine();
			}
		}

		static void RunBot()
		{
			FriendsNearbySystem system = new FriendsNearbySystem();
			var userService = system.GetService<IUsersService>();
			var peopleService = system.GetService<IPeopleNearbyService>();
			var locationService = system.GetService<ILocationService>();

			BotProcessor processor = new BotProcessor(
				ConfigurationManager.AppSettings["botUser"],
				ConfigurationManager.AppSettings["botPassword"],
				new Greeter(),
				new DefaultProcessor(),
				new HelpProcessor(),
				new HiProcessor(),
				new NearbyNotifierProcessor(userService, locationService, peopleService),
				new FriendsProcessor(userService, peopleService),
				new PeopleNearbyProcessor(userService, peopleService),
				new FriendsQueryProcessor(userService, peopleService),
				new PingProcessor(userService, locationService),
				new UpdateLocationProcessor(userService, locationService));

			processor.Run();

			Console.WriteLine("Done");
		}

		static void BotAlreadyRunning()
		{
			Console.WriteLine("This application is already running");
		}
	}
}
